﻿using Microsoft.Win32;

namespace System.IO {
	public static class PathExtensions {
		/// <summary>
		/// 	Retrieve the mimetype for the specified filename.
		/// </summary>
		/// <param name = "fileName">Name of the file.</param>
		/// <returns></returns>
		public static string GetMimeType (this string fileName ) {
			string mime = "application/octetstream";
			string ext = Path.GetExtension ( fileName ).ToLower ( );
			RegistryKey rk = Registry.ClassesRoot.OpenSubKey ( ext );
			if ( rk != null && rk.GetValue ( "Content Type" ) != null ) {
				mime = rk.GetValue ( "Content Type" ).ToString ( );
			}
			return mime;
		}

		/// <summary>
		/// 	Retrieve the mimetype for the specified filename.
		/// </summary>
		/// <param name = "fl">The fl.</param>
		/// <returns></returns>
		public static string GetMimeType ( this FileInfo fl ) {
			string mime = "application/octetstream";
			RegistryKey rk = Registry.ClassesRoot.OpenSubKey ( fl.Extension );
			if ( rk != null && rk.GetValue ( "Content Type" ) != null ) {
				mime = rk.GetValue ( "Content Type" ).ToString ( );
			}
			return mime;
		}
	}
}